import { _decorator, Component, Node, randomRangeInt, SpriteFrame } from 'cc';
import { JJWXR_Enemy } from './JJWXR_Enemy';
import { JJWXR_Player } from './JJWXR_Player';
import { eventCenter } from '../Utils/JJWXR_EventCenter';
import { JJWXR_Events } from '../Utils/JJWXR_Events';
import { JJWXR_PicManager } from './UI/JJWXR_PicManager';
const { ccclass, property } = _decorator;

@ccclass('JJWXR_EnemyManager')
export class JJWXR_EnemyManager extends Component {
    private enemyNode: Node[] = [];

    public enemyPic: SpriteFrame = null;

    private static _instance: JJWXR_EnemyManager;
    public static get instance() {
        return this._instance;
    }
    onLoad() {
        JJWXR_EnemyManager._instance = this;
    }

    start() {
        this.enemyNode = this.node.children;
        eventCenter.on(JJWXR_Events.ENEMY_WORLDPOSITION, this.getEnemyWorldPos, this);
        eventCenter.on(JJWXR_Events.ENEMY_PICTURE, this.showEnemyPic, this);
    }

    onDestroy() {
        eventCenter.off(JJWXR_Events.ENEMY_WORLDPOSITION, this.getEnemyWorldPos, this);
        eventCenter.off(JJWXR_Events.ENEMY_PICTURE, this.showEnemyPic, this);
    }

    // 改变敌人的材质
    getEnemyWorldPos() {
        for (let i = 0; this.enemyNode.length; i++) {
            if (i == this.enemyNode.length) {
                //没有可显示的敌人
                console.log("没有可显示的敌人");
                i--;
                break;
            }
            let enemy = this.enemyNode[i].getComponent(JJWXR_Enemy);
            if (!enemy.isDie) {
                console.log("敌人的世界坐标" + enemy.node.worldPosition);
                JJWXR_Player.instance.lookAtTargetPosition(enemy.node);
                break;
            } else {
                continue;
            }
        }
    }

    // 更换敌人的角色
    showEnemyPic() {
        for (let i = 0; this.enemyNode.length; i++) {
            if (i == this.enemyNode.length) {
                //没有可显示的敌人
                console.log("没有可显示的敌人");
                i--;
                break;
            }
            let enemy = this.enemyNode[i].getComponent(JJWXR_Enemy);
            if (!enemy.isDie) {
                console.log("更换敌人的角色" + enemy.name);
                this.enemyPic = enemy.getEnemyPhoto();
                console.log("敌人角色图片" + this.enemyPic.name);
                JJWXR_PicManager.instance.setSpriteFrame(this.enemyPic);
                break;
            } else {
                continue;
            }
        }
    }
}